﻿package bdz.common.display{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.SecurityDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.utils.*;
	import flash.events.*;
	import flash.display.MovieClip;
	public class DisplayLoader extends MovieClip {
		public var holder:Loader;
		protected var defURL:String;
		protected var loadContext:LoaderContext;
		protected var _autoSize:Boolean = false;
		protected var bg:MovieClip;
		protected var mwidth:Number = 50;
		protected var mheight:Number = 50;
		protected var _loaded:Boolean = false;
		protected var preloader:MovieClip;
		protected var preloaderRef:Object;
		public var smoothenBitmap:Boolean = false;
		private var smooth:Bitmap;
		public function DisplayLoader(defImage:String = null) {
			super();
			defURL = defImage;
			initializeComponent();

		}
		protected function initializeComponent(evt:*=null):void {
			
			//bg = new MovieClip();
			//createBounds(bg, mwidth, mheight, 0);
			holder = new Loader();
			loadContext = new LoaderContext(false);
			try{
				loadContext.checkPolicyFile = false;
				Security.allowDomain("*");
			}catch(e:Error){
				//trace("?????>>>"+e.message);
			}
			configureListener(holder.contentLoaderInfo);
			//addChild(bg);
			addChild(holder);
			if (defURL) {
				holder.load(new URLRequest(defURL), loadContext);
			}
			addEventListener(Event.UNLOAD, clearItUp);
		}
		protected function completeHandler(evt:Event):void {
			
			_loaded = true;
			try {
				removeChild(preloader);
			} catch (e:Error) {

			}
			
			dispatchEvent(evt);
		}
		protected function httpStatusHandler(evt:HTTPStatusEvent):void {

			dispatchEvent(evt);
		}
		protected function initHandler(evt:Event):void {
			
			if (_autoSize) {
				holder.width = mwidth;
				holder.height = mheight;
			}
			if (smoothenBitmap) {
				try{
					holder.visible = false;
					smooth = new Bitmap(bitmap, "auto", true);
					addChild(smooth);
				}catch(e:Error){
					
				}

			}
			dispatchEvent(evt);
		}
		protected function ioErrorHandler(evt:IOErrorEvent):void {
			
			dispatchEvent(evt);
		}
		protected function openHandler(evt:Event):void {
			if (preloaderRef) {
				preloader = new preloaderRef();
				preloader.x = width/2;
				preloader.y = height/2;
				addChild(preloader);
			}
			dispatchEvent(evt);
		}
		protected function progressHandler(evt:ProgressEvent):void {

			dispatchEvent(evt);
		}
		protected function unLoadHandler(evt:Event):void {
			dispatchEvent(evt);
		}
		protected function clearItUp(evt:Event):void {

		}
		private function configureListener(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
			dispatcher.addEventListener(Event.INIT, initHandler, false, 0, true);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
			dispatcher.addEventListener(Event.OPEN, openHandler, false, 0, true);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
			dispatcher.addEventListener(Event.UNLOAD, unLoadHandler, false, 0, true);
		}
		protected function createBounds(clip:MovieClip,w:Number,h:Number,a:Number) {
			clip.graphics.lineStyle(0,0x000000,0);
			clip.graphics.beginFill(0x000000,a);
			clip.graphics.moveTo(0,0);
			clip.graphics.lineTo(0,0);
			clip.graphics.lineTo(w,0);
			clip.graphics.lineTo(w,h);
			clip.graphics.lineTo(0,h);
			clip.graphics.lineTo(0,0);
			clip.graphics.endFill();
		}
		public function unload():void {
			
			holder.unload();
			removeChild(holder);
			try{
				removeChild(smooth);
			}catch(e:Error){}
			holder = new Loader();
			loadContext = new LoaderContext();
			try{
				loadContext.checkPolicyFile = true;
				Security.allowDomain("*");
			}catch(e:Error){
				
			}
			configureListener(holder.contentLoaderInfo);
			//addChild(bg);
			addChild(holder);
		}
		public function get content():DisplayObject{
			
			if(smoothenBitmap){
			
				return smooth as DisplayObject;
			}
			
			return holder.content as DisplayObject;
		}
		
		public function set source(src:String):void {
			if(src){
				defURL = src;
				_loaded = false;
				unload();
				holder.load(new URLRequest(defURL));
			}
		}
		/*
		public function get context():LoaderContext{
			
		}*/
		public function get source():String {
			return defURL;
		}
		public function set autoSize(b:Boolean):void {
			_autoSize = b;
		}
		public function get autoSize():Boolean {
			return _autoSize;
		}
		public function set imgWidth(n:Number):void {
			if (loaded && _autoSize) {
				holder.width = n;
			}
			mwidth = n;
		}
		public function get imgWidth():Number {
			return mwidth;
		}
		public function set imgHeight(n:Number):void {
			if (loaded && _autoSize) {
				holder.height = n;
			}
			mheight = n;
		}
		public function get imgHeight():Number {
			return mheight;
		}
		public function abort():void {
			try {
				holder.close()
			}catch (e:Error) { };
		}
		public function cleanUp():void {
			try{
				try{
					smooth.bitmapData.dispose();
				}catch(e:Error){}
				holder.unload();
				holder.close();
				removeChild(holder);
				holder = null;
			}catch (e:Error) {
				
			}
		}
		public function usePreloader(p:String):void {
			preloaderRef = getDefinitionByName(p);
		}
		public function get bitmap():BitmapData {
			
			var bmp:BitmapData = new BitmapData(holder.width, holder.height, true, 0x00FFFFFF);
			bmp.draw(holder);
			return bmp;
		}
		public function get loaded():Boolean {
			return _loaded;
		}
	}
}